﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TinyMoocSite.Helpers;
using TinyMoocSite.Models;
using TinyMoocSite.ViewModels;

namespace TinyMoocSite.Filters
{
    public class HeaderFooterFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResult v = filterContext.Result as ViewResult;
            if (v != null)      // v will null when v is not a ViewResult
            {
                BaseViewModel bvm = v.Model as BaseViewModel;
                if (bvm != null)
                {
                    UserInfoHelper helper = new UserInfoHelper();
                    UserInfo userInfo = helper.GetUserInfoByUsername(HttpContext.Current.User.Identity.Name);
                    if (userInfo != null)
                    {
                        bvm.FullName = userInfo.FullName;
                        bvm.LicenseId = userInfo.LicenseId;
                    }
                    else
                    {
                        // Should not possible.
                        bvm.FullName = "Invalid User";
                        bvm.LicenseId = "Invalid Id";
                    }
                }
            }
        }
    }
}